﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : TianTeng
// Created          : 09-03-2018
//
// Last Modified By : TianTeng
// Last Modified On : 09-03-2018
// ***********************************************************************
// <copyright file="ModelElementDbUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System.Linq;
using OSS.BIM.Model;
using ZFGK.Utility;

namespace OSS.BIM.Business.Utility
{
    /// <summary>
    /// Class ModelElementDbUtil.
    /// </summary>
    public static class ModelElementDbUtil
    {
        /// <summary>
        /// Gets at.
        /// </summary>
        /// <param name="modelElementId">The model information identifier.</param>
        /// <returns>ModelElement.</returns>
        public static ModelElement GetAt(long modelElementId)
        {
            return DB.Context.From<ModelElement>().Where(d => d.ModelElementId == modelElementId).First();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <param name="modelGrope">The model grope.</param>
        /// <returns>ModelElement[].</returns>
        private static ModelElement[] GetModelInfos(this ModelGrope modelGrope)
        {
            return DB.Context.From<ModelElement>().Where(d => d.ModelGropeId == modelGrope.ModelGropeId).ToList()
                .ToArray();
        }

        /// <summary>
        /// Gets the model infos simple.
        /// </summary>
        /// <param name="modelGrope">The model grope.</param>
        /// <returns>ModelElement[].</returns>
        public static ModelElement[] GetSimpleModelInfos(this ModelGrope modelGrope)
        {
            return DB.Context.From<ModelElement>().Select(d => new { d.ModelElementId, d.ModelGropeId, d.Name }).Where(d => d.ModelGropeId == modelGrope.ModelGropeId).ToList()
                .ToArray();
        }

        /// <summary>
        /// News the identifier if not set.
        /// </summary>
        /// <param name="modelElement">The model element.</param>
        public static void NewIdIfNotSet(this ModelElement modelElement)
        {
            if (modelElement.ModelElementId == 0)
                modelElement.ModelElementId = SnowflakeUtil.GetId();
        }

        /// <summary>
        /// Inserts the specified model grope.
        /// </summary>
        /// <param name="modelElement">The model grope.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(ModelElement modelElement)
        {
            modelElement.NewIdIfNotSet();
            return DB.Context.Insert(modelElement) > 0;
        }

        /// <summary>
        /// Inserts the specified model gropes.
        /// </summary>
        /// <param name="modelElements">The model gropes.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(IEnumerable<ModelElement> modelElements)
        {
            modelElements.ForEach(d => d.NewIdIfNotSet());
            return DB.Context.Insert(modelElements) > 0;
        }

        /// <summary>
        /// Gets the last identifier.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public static long GetLastId()
        {
            return DB.Context.From<ModelElement>().Select(d => d.ModelElementId)
                .OrderByDescending(ModelElement._.ModelElementId).Top(1).First().ModelElementId;
        }

        /// <summary>
        /// Gets the last ids.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns>System.Int32[].</returns>
        public static long[] GetLastIds(int count)
        {
            return DB.Context.From<ModelElement>().Select(d => d.ModelElementId)
                .OrderByDescending(ModelElement._.ModelElementId).Top(count).ToList().Select(d => d.ModelElementId).Reverse().ToArray();
        }

        /// <summary>
        /// Gets the last.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns>ModelElement[].</returns>
        public static ModelElement[] GetLast(int count)
        {
            return DB.Context.From<ModelElement>()
                .OrderByDescending(ModelElement._.ModelElementId).Top(count).ToList().ToArray().Reverse().ToArray();
        }

        /// <summary>
        /// Gets the model grope.
        /// </summary>
        /// <param name="modelElement">The model information.</param>
        /// <returns>ModelGrope.</returns>
        public static ModelGrope GetModelGrope(this ModelElement modelElement)
        {
            return ModelGropeDbUtil.GetAt(modelElement.ModelGropeId);
        }
    }
}
